<?php
namespace App\Repositories\Goods;

use App\Repositories\BaseRepository;

class GoodsModel
{
    use BaseRepository;

    protected static $table = 'goods';

    /**
     * 获取列表并分页 支持 where 和 whereIn
     * User: zhouyao
     * Date: 2018/8/27
     * Time: 上午10:16
     * @param $nowPage
     * @param $offset
     * @param string $where
     * @param string $field
     * @param string $asc
     * @param string $fileName
     * @param $data
     * @return bool
     */
    public function getWhereInPageData($nowPage, $offset, $where = '', $field = '', $asc='desc', $fileName = '', $data)
    {
        if (empty($nowPage) || empty($offset)) {
            return false;
        }

        $database = self::$db->table(static::$table)->forPage($nowPage, $offset);
        if (!empty($where)) {
            $database = $database->where($where);
        }

        if (!empty($field)) {
            $database = $database->orderBy($field, $asc);
        }

        if (!empty($fileName) && !empty($data)) {
            $database = $database->whereIn($fileName, $data);
        }

        return $database->get();
    }

    /**
     * 获取WhereIn 数据列返回单列
     * User: zhouyao
     * Date: 2018/8/27
     * Time: 上午10:50
     * @param $where
     * @param $fieldName
     * @param $data
     * @param string $pluck
     * @return  array
     */
    public function getWhereInDataPluck($where, $fieldName, $data, $pluck = '')
    {
        if (empty($where)) {
            return false;
        }

        $database = self::$db->table(static::$table)->where($where);

        if (!empty($fieldName) && !empty($data)) {
            $database = $database->whereIn($fieldName, $data);
        }

        return $database->pluck($pluck);
    }
}